// pages/game/game.js
// pages/game/game.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    area:240,
    initArray: [],
    runtimeArray: [],
    ads: [],
    animation: [],
    url: '',
    urlArr: ['../../utils/imgs/1.jpg',
      '../../utils/imgs/2.jpg',
      '../../utils/imgs/3.jpg',
      '../../utils/imgs/4.jpg',
      '../../utils/imgs/5.jpg',
      '../../utils/imgs/6.jpg',
      '../../utils/imgs/7.jpg',
      '../../utils/imgs/8.jpg',],
    lock: -1,
    level: 2,
    items: [
      { value: '菜鸟', name: '2', checked: 'true' },
      { value: '简单', name: '3' },
      { value: '一般', name: '4' },
      { value: '困难', name: '5' },
      { value: '大师', name: '6' },
    ],
    //存储计时器
    setInter: '',
    num: 0,
    quence:0,
    score:0,
    list: [],
  },
  updateList: function (uArr, pArr) {
    var arr = new Array();
    for (var i = 0; i < uArr.length; i++) {
      var o = {
        uid: 0,
        pos: '',
        bian: '',
      }
      o.uid = uArr[i];
      o.pos = pArr[i];
      o.bian = this.data.area / this.data.level + "px";
      arr.push(o)
    }
    this.setData({
      list: arr,
    })
  },
  updateimage: function () {
    var d = Math.floor(Math.random() * this.data.urlArr.length);
    //console.log(d);
    this.setData({
      url: this.data.urlArr[d]
    })
    this.initData();
  },
  chooseimage: function () {
    var _this = this;
    wx.chooseImage({
      //count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // console.log(res.tempFilePaths);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        _this.setData({
          url: res.tempFilePaths[0],
          urlArr: res.tempFilePaths
        })
        _this.initData();
        // var tempFilePaths = res.tempFilePaths
        // wx.uploadFile({
        //   url: 'http://example.weixin.qq.com/upload', //仅为示例，非真实的接口地址
        //   filePath: tempFilePaths[0],
        //   name: 'file',
        //   formData: {
        //     'user': 'test'
        //   },
        //   success: function (res) {
        //     var data = res.data
        //     //do something
        //   }
        // })
      }
    })
  },
  startSetInter: function () {
    var that = this;
    //将计时器赋值给setInter
    that.data.setInter = setInterval(
      function () {
        var numVal = that.data.num + 1;
        that.setData({ num: numVal });
        //console.log('setInterval==' + that.data.num);
      }
      , 1000);
  },
  endSetInter: function () {
    var that = this;
    //清除计时器  即清除setInter
    clearInterval(that.data.setInter)
    that.setData({ num: 0 });
  },
  radioChange: function (e) {
    //console.log(e);
    this.setData({
      level: e.detail.value
    })
    this.initData();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      quence:Math.floor(Math.random()*2000)+10000
    })
    this.updateimage();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  initData: function () {
    //清除计时器  即清除setInter
    this.endSetInter();
    this.startSetInter();
    var data = new Array();
    var count = this.data.level * this.data.level;
    for (var i = 0; i < count; i++) {
      var flag = false;
      var r = 0;
      do {
        r = Math.floor(Math.random() * 100) % count;
        for (var j = 0; j < data.length; j++) {
          if (r == data[j]) {
            flag = true;
            break;
          } else {
            flag = false;
          }
        }
      } while (flag)
      data.push(r);
    }
    //console.log(data);
    this.setData({
      initArray: data,
      runtimeArray: data
    })

    var arr = new Array();
    for (var it = 0; it < data.length; it++) {
      if (data[it] == 0) {
        arr.push('0% 0%');
      } else if (data[it] == data.length-1) {
        arr.push('100% 100%')
      } else {
        let x = -(data[it] % this.data.level) * this.data.area / this.data.level;
        let y = -(Math.floor(data[it] / this.data.level)) * this.data.area / this.data.level;
        arr.push(x + 'px ' +y+'px');
      }
    }
    //for (var it = 0; it < data.length; it++) {
    // if(count==4){
    // if (data[it] == 1) {
    //   arr.push('0% 0%');
    // } else if (data[it] == 2) {
    //   arr.push('100% 0%')
    // } else if (data[it] == 3) {
    //   arr.push('0% 100%')
    // } else {
    //   arr.push('100% 100%')
    // }
    // }else if(count==9){
    //   if (data[it] == 1) {
    //     arr.push('0% 0%');
    //   } else if (data[it] == 2) {
    //     arr.push('50% 0%')
    //   } else if (data[it] == 3) {
    //     arr.push('100% 0%')
    //   } else if (data[it] == 4){
    //     arr.push('0% 50%')
    //   } else if (data[it] == 5) {
    //     arr.push('50% 50%');
    //   } else if (data[it] == 6) {
    //     arr.push('100% 50%')
    //   } else if (data[it] == 7) {
    //     arr.push('0% 100%')
    //   } else if ((data[it] == 8)){
    //     arr.push('50% 100%')
    //   } else {
    //     arr.push('100% 100%')
    //   }
    // }else{
    //   if (data[it] == 1) {
    //     arr.push('0% 0%');
    //   } else if (data[it] == 2) {
    //     arr.push('-60px 0%')
    //   } else if (data[it] == 3) {
    //     arr.push('-120px 0%')
    //   } else if (data[it] == 4) {
    //     arr.push('100% 0%')
    //   } else if (data[it] == 5) {
    //     arr.push('0% -60px');
    //   } else if (data[it] == 6) {
    //     arr.push('-60px -60px')
    //   } else if (data[it] == 7) {
    //     arr.push('-120px -60px')
    //   } else if (data[it] == 8) {
    //     arr.push('100% -60px')
    //   } else if (data[it] == 9) {
    //     arr.push('0% -120px')
    //   } else if (data[it] == 10) {
    //     arr.push('-60px -120px');
    //   } else if (data[it] == 11) {
    //     arr.push('-120px -120px')
    //   } else if (data[it] == 12) {
    //     arr.push('100% -120px')
    //   } else if (data[it] == 13) {
    //     arr.push('0% 100%')
    //   } else if (data[it] == 14) {
    //     arr.push('-60px 100%');
    //   } else if (data[it] == 15) {
    //     arr.push('-120px 100%')
    //   } else{
    //     arr.push('100% 100%')
    //   }
    // }
    //}
    this.setData({
      ads: arr
    })
    this.updateList(data, arr);
    this.banduan();
  },
  /**
   * 图片点击事件
   */
  click: function (event) {
    if (this.data.lock == -1) {
      this.setData({
        lock: event.target.dataset.uid
      })
    } else {
      var arr = this.data.ads;
      var iarr = this.data.initArray;
      var x, y;
      for (var i = 0; i < iarr.length; i++) {
        if (iarr[i] == this.data.lock)
          x = i;
        if (iarr[i] == event.target.dataset.uid)
          y = i;
      }
      var temp = arr[x];
      arr[x] = arr[y];
      arr[y] = temp;
      iarr[x] = event.target.dataset.uid;
      iarr[y] = this.data.lock;

      this.setData({
        ads: arr,
        initArray: iarr,
        lock: -1,
      })
      this.updateList(iarr, arr);
      //console.log(this.data.runtimeArray);
      //console.log(this.data.initArray);
      this.banduan();
    }
  },
  banduan: function () {
    var f = true;
    for (var t = 0; t < this.data.initArray.length; t++) {
      if (this.data.initArray[t] != t )
        f = false;
    }
    if (f) {
      f = false;
      this.showok();
    }
  },
  showok: function () {
    let _this=this;
    wx.showToast({
      title: '成功,用时' + this.data.num + '秒',
      icon: 'success',
      duration: 2000
    })
    let a = (this.data.score + this.data.level * 30 - Math.floor(this.data.num / 10) * 5) < 0 ? 0 : (this.data.score + this.data.level * 30 - Math.floor(this.data.num / 100) * 10)
    let b = (this.data.quence - Math.floor(this.data.score / 5)) < 1 ? 1 : (this.data.quence - Math.floor(this.data.score / 5));
    this.setData({
      score:a,
      quence:b
    })
    setTimeout(function(){
      _this.endSetInter();
      _this.updateimage();
    },2000);
  }
})